// 云对象教程: https://uniapp.dcloud.net.cn/uniCloud/cloud-obj

// jsdoc语法提示教程：https://ask.dcloud.net.cn/docs/#//ask.dcloud.net.cn/article/129
const db = uniCloud.database();
const tally = db.collection('tally')
const dbCmd = db.command

module.exports = {
  // const db = uniCloud.database();
  _before: function() { // 通用预处理器

  },
  /**删除一条记录
   * @param {Object} id 记录id
   */
  async del(id) {
    let res = await tally.where({
      _id: id
    }).remove()
    if (res.deleted == 1) {
      return {
        code: 200,
        message: 'OK'
      }
    } else {
      return {
        errCode: 100,
        errMsg: '失败了'
      }
    }
  },


  /**
   * 添加一条记录
   * @param {object} obj 一个添加记录的对象
   * @returns {object} code 200 成功
   */
  async add(obj) {
    // 参数校验，如无参数则不需要
    let res = await tally.add(obj)
    if (res.id) {
      return {
        code: 200
      }
    } else {
      return {
        errCode: 100,
        errMsg: '创建失败'
      }
    }
  },
  /**
   * 修改数据
   * @param {Object} id
   * @param {Object} obj
   */
  async updata(id, obj) {
    let res = await tally.where({
      _id: id,
    }).update(obj)
    if (res.updated == 1) {
      return {
        code: 200,
        message: 'OK'
      }
    } else {
      return {
        errCode: 100,
        errMsg: '失败了'
      }
    }
  },

  /**
   * 获取一个周的数据
   * @param {Array} week 一个周的日期 [{year:'2022',month:'11',dates:'05'},{year:'2022',month:'11',dates:'05'}]
   */
  async getWeekData(date) {
    // let method = uniCloud.importObject('tally')
    function fun(str) {
      return date.map(v => {
        return v[str]
      })
    }
    let year = fun('year')
    let month = fun('month')
    let day = fun('day')
    let weekArray = []
    // console.log(year);
    let res = await tally.where({
      year: dbCmd.in(year),
      month: dbCmd.in(month),
      day: dbCmd.in(day),
    }).get()
    // console.log(res);
    if (res.affectedDocs >= 0) {
      return {
        code: 200,
        data: res.data
      }
    } else {
      return {
        errCode: 100,
        errMsg: '失败了'
      }
    }
  },
  // 获取一天的数据
  async getDate(year, month, day) {
    let res = await tally.where({
      year,
      month,
      day,
    }).get()
    // console.log(res);
    if (res.affectedDocs > 0) {
      return res.data
    } else {
      return [{
        year,
        month,
        day
      }]
    }
  },


  /**
   * 获取月份的数据
   * @param {object} month 月份
   * @returns {object} data 数据列表
   */
  async getMonthData(year, month) {
    let res = await tally.where({
      year,
      month,
    }).get()
    if (res.affectedDocs >= 0) {
      return {
        code: 200,
        data: res.data
      }
    } else {
      return {
        errCode: 100,
        errMsg: '失败了'
      }
    }
  },

  /**
   * 获取一年的数据
   * @param {Object} year
   * @param {Object} type
   */
  async getYearData(year) {
    let res = await tally.where({
      year,
    }).get()
    if (res.affectedDocs >= 0) {
      return {
        code: 200,
        data: res.data
      }
    } else {
      return {
        errCode: 100,
        errMsg: '失败了'
      }
    }
  },
}
